package com.huahua.api.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.huahua.api.common.constant.Consant;
import com.huahua.api.entity.param.RandomCharacterParam;
import com.huahua.api.service.RandomCharacterService;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.RandomUtil;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

/**
 * @author LoveHuaHua
 * @date 2023年08月19日 22:15
 * @description believe in yourself
 */
@Service
public class RandomCharacterServiceImpl implements RandomCharacterService {

    private static final List<String> NUMBER_CHARACTER_LIST = Lists.newArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
    private static final List<String> LOWER_CHARACTER_LIST = Lists.newArrayList(
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
            "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
    );

    private static final List<String> UPPER_CHARACTER_LIST = Lists.newArrayList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
            "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
    private static final List<String> SPECIAL_CHARACTER_LIST = Lists.newArrayList(
            "!", "@", "#", "$", "%", "^", "&", "*", "-", "_", "=", "+"
    );

    /**
     * 生成随机字符
     *
     * @param param 参数
     * @return {@link String}
     */
    @Override
    public String generate(RandomCharacterParam param) {
        HashSet<String> objects = Sets.newHashSet();
        if (param.getChooseNumber().equals(Consant.YesOrNoConstant.YES)){
            objects.addAll(NUMBER_CHARACTER_LIST);
        }
        if (param.getChooseLowerLetter().equals(Consant.YesOrNoConstant.YES)){
            objects.addAll(LOWER_CHARACTER_LIST);
        }

        if (param.getChooseUpperLetter().equals(Consant.YesOrNoConstant.YES)){
            objects.addAll(UPPER_CHARACTER_LIST);
        }

        if (param.getChooseSpecial().equals(Consant.YesOrNoConstant.YES)){
            objects.addAll(SPECIAL_CHARACTER_LIST);
        }
        String join = StrUtil.join("", objects);
        String randomStr = RandomUtil.randomString(join, param.getLength());

        // 如果有前缀，添加到随机字符串前面
        if (StrUtil.isNotBlank(param.getPrefix())) {
            return param.getPrefix() + randomStr;
        }

        return randomStr;
    }
}
